Skip to content

Conversation

nitrofski
Copy link

Which problem is this PR solving?

Fixes #1506.

Short description of the changes

Enable support for installing field instrumentation on union types.

The "type unwrap" logic was generalized to support unwrapping to a list of types. It is now a recursive function rather than a loop. The new unwrapping has been implemented in a way that is more agreeable to TypeScript.

@nitrofski nitrofski requested a review from a team as a code owner July 3, 2025 16:35
Copy link

linux-foundation-easycla bot commented Jul 3, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@github-actions github-actions bot requested a review from obecny July 3, 2025 16:35
@nitrofski nitrofski changed the title fix union types Enable union type support in @opentelemetry/instrumentation-graphql (#1506) Jul 3, 2025
@nitrofski nitrofski changed the title Enable union type support in @opentelemetry/instrumentation-graphql (#1506) fix: Enable union type support in @opentelemetry/instrumentation-graphql (#1506) Jul 3, 2025
@david-luna
Copy link
Contributor

Hi @nitrofski and thanks for your contribution.

I order to proceed we need a couple of things:

  • a recent PR which changes the folder structure has ben merged. Please sync your PR with the latest changes
  • you need to sing the CLA so we can run the CI

Cheers

@dyladan
Copy link
Member

dyladan commented Jul 9, 2025

@obecny can you please take a look at this?

@nitrofski
Copy link
Author

nitrofski commented Jul 9, 2025

@david-luna

I merged with main.

There are issues with CLA approval at my company at the moment, which we are working on getting resolved. I will sign as soon as that's done. Sorry for the delay.

@nitrofski
Copy link
Author

nitrofski commented Jul 10, 2025

CLA issues have been resolved.

@dyladan
Copy link
Member

dyladan commented Jul 16, 2025

@obecny can you please take a look at this?

@nitrofski
Copy link
Author

Good day,

This hasn't seen feedback in over a month. Is there anything I can do to help get attention on this PR?

Copy link

codecov bot commented Sep 12, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.45%. Comparing base (9d689e1) to head (0c5ef73).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2923   +/-   ##
=======================================
  Coverage   91.44%   91.45%           
=======================================
  Files         146      146           
  Lines        8194     8202    +8     
  Branches     1846     1848    +2     
=======================================
+ Hits         7493     7501    +8     
  Misses        701      701           
Flag Coverage Δ
instrumentation-graphql 94.10% <100.00%> (+0.10%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
packages/instrumentation-graphql/src/utils.ts 93.45% <100.00%> (+0.25%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@@ -305,11 +305,7 @@ export function wrapFields(
tracer: api.Tracer,
getConfig: () => GraphQLInstrumentationParsedConfig
): void {
if (
!type ||
typeof type.getFields !== 'function' ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar enough with this library. Are we certain that all possible values of type have a function named getFields?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is part of graphqlTypes.GraphQLObjectType. As long as we maintain type safety and that the type definitions are accurate (to my testing, they were), we should be safe.

The previous implementation used an any type before recursing, so they lost type safety and ended up passing non-compliant object. It had to re-assess the object here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay then


// unwrap union types
if (isGraphQLUnionType(type)) {
return type.getTypes();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I have a similar question. Are we certain that getTypes() returns an array of GraphQLObjectType. Could be possible to have nested unions?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair concern. Doing a quick search through the graphql type definitions, GraphQLUnionType is the only type with a getTypes() function, and it always returns an array of GraphQLObjectType. That all being said, is it enough for us or do we want to be extra safe and also validate what the function returns at runtime?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be possible to have nested unions?

Doing a bit of research...

From the Apollo Graphql Union and Interfaces schema help page

All of a union's included types must be object types (not scalars, input types, etc.). Included types do not need to share any fields.

It sounds like unions strictly require their components to be objects and that nested union are not allowed. Though, to dispel any doubts, I tried launching my graphql server with a dummy union type that uses other union types:

Union type MyTestUnion can only include Object types, it cannot include <other union type>.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for your thorough explanation @nitrofski :)

@david-luna
Copy link
Contributor

david-luna commented Sep 15, 2025

@nitrofski thank for resolving my doubts. You have my approval but I'll wait a bit in case someone else wants to do a review. @obecny maybe?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

GraphQL field instrumentation does not work for union return types
4 participants